// pages/write/detail.js
var app = getApp();
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    detail: '',
    liked: false,
    likecount: '',
    readcount: '',
    content: '',
    arr: '',
    StartTime: '',
    EndTime: '',
    imgss: '',
    openid:'',
  },
  previewImagess(e) {
    var current = e.target.dataset.src;
    console.log(current);
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: this.data.imgss
    })

  }, //预览图片

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var opid = wx.getStorageSync('user').openid;
    console.log(opid, 'openid')
    that.setData({
      id: options.id,
      openid:opid
    })
    that.read();
    that.detail();

  },
  detail() {
    wx.showLoading({            
      title: '加载中',
                
    })
    var that = this;
    var like = that.data.liked;
    var id = that.data.id;
		
    var opid = wx.getStorageSync('user').openid;
    wx.showLoading({
      title: '加载中',
    })
    app.core('get', 'xiaochengxu_article_detail/id/' + id + '', {}, (res => {

      var arr = res.data;
      wx.hideLoading()
      var content = arr.content;
      var x = WxParse.wxParse('article', 'html', content, that, 5);

      arr.content = x;


      var commlist = res.data.xiaochengxu_article_comment;

      var commentarr = [];
      for (var i = 0; i < commlist.length; i++) {
        if (commlist[i].p_comment_id == 0) {
          console.log(commlist[i])
          commentarr.push(commlist[i])
        }

      }

      var imglist = arr.album;
      var xx = imglist.split(',');

      that.setData({
        detail: arr,
        arr: commentarr,
        imgss: xx
      })
      wx.hideLoading()
    }))
  }, // 详情

  btnlikes() {
    var that = this;
    var like = that.data.liked;
    var id = that.data.id;
    var opid = wx.getStorageSync('user').openid;
    app.core('get', 'operate_like/article_id/' + id + '/open_id/' + opid + '', '', (res => {
      var islike = res.data.isLike;
      var likecount = res.data.count;

      if (islike == 0) {
        that.setData({
          liked: false,
          likecount: likecount
        })
        wx.showToast({
          title: '取消点赞',
          icon: 'none',
          duration: 1000
        })
      } else if (islike == 1) {
        that.setData({
          liked: true,
          likecount: likecount
        })
        wx.showToast({
          title: '点赞成功',
          icon: 'none',
          duration: 1000
        })
      }
    }))
  },
  //点赞
  read() {
    var that = this;
    var id = that.data.id;
    var opid = wx.getStorageSync('user').openid;
    app.core('get', 'operate_vist/id/' + id + '', '', (res => {
      console.log(res, 'res')

      that.setData({
        readcount: res.data.count,

      })
    }))
  },
  //访问

  priveimg(e) {
    console.log(e)
    var img = e.currentTarget.dataset.img;
    var arr = [];
    arr.push(img)
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: arr // 需要预览的图片http链接列表
    })
  },
  //预览图片
  /**

   * 
  /**
   * 
   * 生命周期函数--监听页面初次渲染完成
   */

  input(e) {
    this.setData({
      content: e.detail.value
    })

  },
  submit() {
    var that = this;
    var content = that.data.content;
    // var p_comment_id= ''; // 评论文章的id
		var opids = wx.getStorageSync('user');//存入全局变量
		var opid = opids.openid;
		console.log(opid, '存储的obj啊');
    // var opid = wx.getStorageSync('user').openid;
    var tid = that.data.id;
    if (content == '') {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none',
        duration: 1000
      })
    } else {
      console.log(content)
      wx.showLoading({
        title: '提交中',
      })
      app.core('post', 'create_article_comment', {

        open_id: opid,
        article_id: tid,
        p_xiaochengxu_user_id: 0,
        comment: content,
        p_comment_id: 0,

      }, (res => {
        console.log(res)
        if (res.resultCode == 200) {
          wx.hideLoading()
          wx.showToast({
            title: '评论成功',
            icon: 'none',
            duration: 1000
          })
          setTimeout(function() {
            var sid = that.data.id;

            // var options ={
            // 	id:sid
            // }
            // that.onLoad(options)
          }, 1000)
        }
      }))

    }
  },

  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var timestamp = Date.parse(new Date()); //进入页面的时间戳
    var Stimestamp = timestamp / 1000;
    this.setData({
      StartTime: Stimestamp
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    var timestamp = Date.parse(new Date()); //离开页面的的时间戳
    var Etimestamp = timestamp / 1000;
    this.setData({
      EndTime: Etimestamp
    });
    app.core('post', 'create_data_statistics', {
        // data 部分
        'type': 3,
        'visit_time': this.data.EndTime - this.data.StartTime,
        'read_id': this.data.id,
        'open_id': wx.getStorageSync('user').openid
      }, (res) => {
        console.log(res)
      },
      (err) => {
        console.log(err)
      }
    )
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this;
    var id = that.data.id
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    var shareObj = {
      title: that.data.detail.title, // 默认是小程序的名称(可以写slogan等)
      path: 'pages/write/detail?id=' + id + '',
      imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function(res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {
          wx.showToast({
            title: '转发成功',
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function() {
        // 转发失败之后的回调
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          // 用户取消转发
          wx.showToast({
            title: '已取消转发',
            icon: 'none',
            duration: 1000
          })
        } else if (res.errMsg == 'shareAppMessage:fail') {
          // 转发失败，其中 detail message 为详细失败信息
          wx.showToast({
            title: '转发失败',
            icon: 'none',
            duration: 1000
          })
        }
      },
    }
  }
})